var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableNum:"1",
    time: '',
    ischeck:true,
    totalPrice:'',//原价
    newtotalPrice:'',//最终价格
    discounts:'-￥0',//优惠信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var time = app.globalData.xstime;//未来2021-08-12 10:00:00
    var t = Math.round(new Date() / 1000);//现在时间
      //  console.log(t);
    this.setData({
      ischeck:t<time?true:false,
    })
    // console.log(this.data.ischeck);

    if(app.globalData.scene){
      this.setData({
        tableNum: app.globalData.scene
      })
    }
    // 当前时间
    let date = new Date();
    let year = date.getFullYear();
    let month = (date.getMonth() + 1).toString().padStart(2, '0');
    let day = date.getDate().toString().padStart(2, '0');
    let hour = date.getHours().toString().padStart(2, '0');
    let minute = date.getMinutes().toString().padStart(2, '0');
    let second = date.getSeconds().toString().padStart(2, '0');
    let ymdtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second

    // console.log(ymdtime)
    let appGlobalArr = app.globalData.categoryMenuList
    // console.log(appGlobalArr);

    let orderAdd = []
    for (let i = 0; i < appGlobalArr.length; i++) {
      for (let j = 0; j < appGlobalArr[i].menuList.length; j++) {
        if (appGlobalArr[i].menuList[j].menu_num >= 1) {
          orderAdd.push(appGlobalArr[i].menuList[j])
        }
      }
    }
    // console.log(orderAdd);

    let totalPrice = 0 //总价
    let count = 0 //单个菜数量
    for (let i = 0; i < orderAdd.length; i++) {
      totalPrice = totalPrice + orderAdd[i].menu_score
      count = count + orderAdd[i].menu_num
    }

    // console.log(orderAdd);
    app.globalData.cart.total = totalPrice
    //价格优惠
    if(totalPrice >= 500){
      this.setData({
        discounts:'七折',
        newtotalPrice: totalPrice*0.7,
      })
    }else if(totalPrice >= 300){
      this.setData({
        discounts:'八折',
        newtotalPrice: totalPrice*0.8,
      })
    }else if(totalPrice >= 230){
      this.setData({
        discounts:'-￥20',
        newtotalPrice: totalPrice-20,
      })
    }else if(totalPrice >= 180){
      this.setData({
        discounts:'-￥15',
        newtotalPrice: totalPrice-15,
      })
    }else if(totalPrice >= 130){
      this.setData({
        discounts:'-￥10',
        newtotalPrice: totalPrice-10,
      })
    }else if(totalPrice >= 80){
      this.setData({
        discounts:'-￥5',
        newtotalPrice: totalPrice-5,
      })
    }else{
      this.setData({
        newtotalPrice: totalPrice,
      })
    }

    this.setData({
      orderAdd: orderAdd,
      totalPrice: totalPrice,
      time: ymdtime,
    })

  },

  // 确认订单
  Tapcashtopay: function (){
    let _that = this
    let appGlobalArr = app.globalData.categoryMenuList
    // console.log(appGlobalArr);

    let orderAdd = []
    for (let i = 0; i < appGlobalArr.length; i++) {
      for (let j = 0; j < appGlobalArr[i].menuList.length; j++) {
        if (appGlobalArr[i].menuList[j].menu_num >= 1) {
          orderAdd.push(appGlobalArr[i].menuList[j])
        }
      }
    }
    // console.log(orderAdd);

    let totalPrice = 0 //总价
    let count = 0 //订单菜的总数量
    for (let i = 0; i < orderAdd.length; i++) {
      totalPrice = totalPrice + orderAdd[i].menu_score
      count = count + orderAdd[i].menu_num
    }
    let newtotalPrice = _that.data.newtotalPrice
    // console.log(count);
    console.log(newtotalPrice)
    let orderRemark = app.globalData.orderRemark //从全局变量获取订单备注
    let userId = app.globalData.userId //从全局变量获取用户信息
    // console.log(orderRemark);
    wx.request({
      url: 'https://orders.linshiqiang.cn/api/order/add?nums=' + count + '&prices=' + newtotalPrice + '&user_id=' + userId + '&table_number=' + this.data.tableNum + '&status=' + 1 +
        '&order_remark=' + orderRemark,
      data: {
        foodList: JSON.stringify(orderAdd)
      },
      success: function (res) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 3000 //持续的时间
        })
      },
      fail: function (res) {
        console.log("失败");
      }
    })

    // console.log(orderAdd);
    app.globalData.cart.total = newtotalPrice
  },

  expInput: function (e) {
    app.globalData.orderRemark = e.detail.value
    this.setData({
      text: e.detail.value
    })
    // console.log( e.detail.value);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})